package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.BleDeviceListAdapter;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.MyBleDeviceListBean;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.http.BaseResult;

import org.greenrobot.eventbus.EventBus;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BleDeviceListActivity extends BaseAppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_ble)
    ImageView ivBle;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.rl_list)
    RecyclerView rlList;
    private MyBleDeviceListBean connected;
    private MyBleDeviceListBean disConnected;
    private List<MyBleDeviceListBean> data;
    private BleDeviceListAdapter bleDeviceListAdapter;

    @Override
    protected void getBundleExtras(Bundle extras) {
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_ble_device_list;
    }

    @Override
    protected void initViewsAndEvents() {
        data = new ArrayList<>();
        connected = new MyBleDeviceListBean(getString(R.string.connected));
        disConnected = new MyBleDeviceListBean(getString(R.string.Ever_paired));
        data.add(connected);
        data.add(disConnected);
        bleDeviceListAdapter = new BleDeviceListAdapter(data, this);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rlList.setLayoutManager(mLayoutManager);
        rlList.setAdapter(bleDeviceListAdapter);
    }

    @Override
    protected void initData() {
        List<MybluetoothDevice> all = LitePal.findAll(MybluetoothDevice.class);
        if (customApplication.isConnected()) {
            List<MybluetoothDevice> devices = new ArrayList<>();
            List<MybluetoothDevice> mybluetoothDevices = LitePal.where("mac = ?", preferenceUtil.getBleMac()).find(MybluetoothDevice.class);
            connected.setMybluetoothDevices(mybluetoothDevices);
            for (int i = 0; i < all.size(); i++) {
                if (!all.get(i).getMac().equals(preferenceUtil.getBleMac())) {
                    devices.add(all.get(i));
                }
            }
            disConnected.setMybluetoothDevices(devices);
        } else {
            connected.setMybluetoothDevices(new ArrayList<>());
            disConnected.setMybluetoothDevices(all);
        }
        bleDeviceListAdapter.notifyDataSetChanged();
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.BLUETOOTH_STATE:
            case Constants.UPGRADEFINISH:
                initData();
                break;
        }

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @OnClick({R.id.iv_back, R.id.iv_ble})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_ble:
                EventBus.getDefault().post(new EventCenter<>(Constants.BLE_CHANGE, preferenceUtil.getBleMac()));
                readyGo(BleActivity.class);
                break;
        }
    }
}
